package com.zyb.system.employee.controller;
import com.zyb.system.employee.domain.Employee;
import com.zyb.system.employee.query.EmployeeQuery;
import com.zyb.system.employee.service.IEmployeeService;
import com.zyb.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
//    @RequestMapping(value = "/list",method = RequestMethod.GET) 方式一

    @GetMapping("/list")//方式二
    @ApiOperation(value = "查询所有员工所有数据")
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    @GetMapping("/listBo")//方式二
    @ApiOperation(value = "查询所有员工部分数据")
    public List<Employee> queryAllByBo(){
        return employeeService.queryAllByDo();
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "查询部门的一条数据")
    //required = true 表示必填项
    public AjaxResult queryOne(@PathVariable Long id){
        try {
           Employee data = employeeService.queryById(id);
            return AjaxResult.success().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取数据失败");
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "部门分页查询")
    public AjaxResult pageList(@RequestBody EmployeeQuery query){
        try {
            List<Employee> page = employeeService.queryPage(query);
            Long count = employeeService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取分页数据失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "新增部门【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.add(employee);
                return AjaxResult.success().setMsg("添加成功");
            }else {
                employeeService.update(employee);
                return AjaxResult.success().setMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("保存数据失败");
    }

    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
}
